﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
namespace _4_1
{
    [Serializable]
    public class Order
    {
        public int num { get; set; }
        public string thing { get; set; }
        public string customer { get; set; }
        public int price { get; set; }
        public Order(int num, string thing, string customer, int price)
        {
            this.num = num;
            this.thing = thing;
            this.customer = customer;
            this.price = price;
        }
        public Order() { }
        /* static public bool Equals()
         {
             bool result=true;
             //wait for sth
             return result;//修改
         }*/
        public void DisplayAnOrder()//输出指定的订单
        {
            Console.WriteLine("订单号:{0}商品名:{1} 顾客:{2}价格:{3}", this.num.ToString(), this.thing.ToString(),
                  this.customer.ToString(), this.price.ToString());
        }

    }
    public class OrderDetails//重写equal函数，来保证添加的订单不重复||每个订单明细不重复
    {
        public bool Equals1(Order order)//details相同
        {
            bool result = true;
            int FindNum;
            foreach (Order anorder in OrderSurvice.MyList)
            {
                if (order.thing == anorder.thing && order.customer == anorder.customer && order.price == anorder.price)
                {
                    result = false;//false表示有相同的值
                    FindNum = order.num;
                    Console.WriteLine("已有相同内容的的订单!订单号是{0}", FindNum);
                    break;
                }
            }
            return result;
        }
        public bool Equals2(Order order)//订单号相同
        {
            bool result = true;
            foreach (Order anorder in OrderSurvice.MyList)
            {
                if (order.num == anorder.num)
                {
                    result = false;
                    Console.WriteLine("已有相同订单号！");
                    break;
                }
            }
            return result;
        }
    }

    //订单号，商品名称，顾客，订单金额
    public class OrderSurvice//一个list中存放订单数据，可以实现订单的增删改查操作
    {
        /*  public int num;
          public string thing;
          public string customer;
          public int price;
          public OrderSurvice(int num,string thing,string customer,int price)
          {
              this.num = num;
              this.thing = thing;
              this.customer = customer;
              this.price = price;
          }*/
        static public List<Order> MyList = new List<Order>();
        XmlSerializer ToXML = new XmlSerializer(typeof(Order[]));
        public void AddList()//增
        {
            int num, price;
            string thing, customer, s;
            Console.WriteLine("订单号：");
            s = Console.ReadLine();
            num = int.Parse(s);
            Console.WriteLine("商品名：");
            thing = Console.ReadLine();
            Console.WriteLine("客户名：");
            customer = Console.ReadLine();
            Console.WriteLine("价格：");
            s = Console.ReadLine();
            price = int.Parse(s);

            Order NewOrder = new Order(num, thing, customer, price);
            OrderDetails orderDetails = new OrderDetails();

            if (orderDetails.Equals2(NewOrder) == false)//前提：没有重复的订单号
                Console.WriteLine("添加失败！");
            else
            if (orderDetails.Equals1(NewOrder) == false)//额外要求：没有内容完全相同的订单
                Console.WriteLine("添加失败！");
            else
            {
                MyList.Add(NewOrder);//list添加元素的标准语句！记住他
                Console.WriteLine("添加订单完成！");
            }

        }
        public void DeleteList()//删
        {
            int n, mybool = 0;
            string s;
            if (MyList.Count == 0)
            {
                Console.WriteLine("当前还没有订单记录，无法删除哦！");
                return;
            }
            Console.WriteLine("要删除的订单号是：");
            s = Console.ReadLine();
            n = int.Parse(s);
            foreach (Order anorder in MyList)
            {
                if (anorder.num == n)
                {
                    MyList.Remove(anorder);
                    Console.WriteLine("删除成功！");
                    mybool = 1;
                    break;
                }
            }
            if (mybool == 0)
                Console.WriteLine("未找到这一订单，删除失败...");
        }
        public void ChangeList()//改
        {
            int n, mybool = 0, changeNum, newnum;
            int newprice;
            string newthing, newcustomer, s;
            Console.WriteLine("要修改的订单号是：");
            s = Console.ReadLine();
            n = int.Parse(s);

            foreach (Order anorder in MyList)
            {
                if (anorder.num == n)
                {
                    Console.WriteLine("找到了这个订单！开始修改：");
                    Console.WriteLine("请输入要修改的内容：订单号1，商品名2，客户名3，价格4");
                    s = Console.ReadLine();
                    changeNum = int.Parse(s);
                    switch (changeNum)
                    {
                        case 1:
                            Console.WriteLine("新的订单号：");
                            s = Console.ReadLine();
                            newnum = int.Parse(s);
                            anorder.num = newnum;
                            mybool = 1;
                            break;
                        case 2:
                            Console.WriteLine("新的商品名：");
                            newthing = Console.ReadLine();
                            anorder.thing = newthing;
                            mybool = 1;
                            break;
                        case 3:
                            Console.WriteLine("新的顾客名：");
                            newcustomer = Console.ReadLine();
                            anorder.customer = newcustomer;
                            mybool = 1;
                            break;
                        case 4:
                            Console.WriteLine("新的价格：");
                            s = Console.ReadLine();
                            newprice = int.Parse(s);
                            anorder.price = newprice;
                            mybool = 1;
                            break;
                        default:
                            Console.WriteLine("修改失败，请输入正确的编号噢");
                            mybool = 0;
                            break;
                    }

                }

            }
            if (mybool == 1) Console.WriteLine("修改成功啦！");
            if (mybool == 0)
                Console.WriteLine("未找到这一订单，删修改失败...");
        }
        public void CheckList()//查
        {
            string s;
            int checkNo, flag = 0;
            int checkNum, checkPrice;
            string checkThing, checkCustomer;
            Console.WriteLine("您想按照什么标准来查找？订单号1，商品名2，客户名3，价格4");
            s = Console.ReadLine();
            checkNo = int.Parse(s);
            List<Order> checkList = new List<Order>();
            switch (checkNo)
            {
                case 1:
                    Console.WriteLine("要查询的订单号：");
                    s = Console.ReadLine();
                    checkNum = int.Parse(s);
                    var checklist1 = MyList.Where(x => x.num == checkNum);

                    //这里得排序,按照订单总金额
                    foreach (Order anorder in checklist1)
                    {
                        checkList.Add(anorder);
                    }
                    SortByPrice(checkList);
                    //排序完成
                    //下面输出
                    foreach (Order anorder in checklist1)
                    {
                        anorder.DisplayAnOrder();
                    }
                    flag = 1;
                    break;
                case 2:
                    Console.WriteLine("要查询的商品名：");
                    checkThing = Console.ReadLine();
                    var checklist2 = MyList.Where(x => x.thing == checkThing);//用linq来实现查找！
                                                                              //这里得排序,按照订单总金额
                    foreach (Order anorder in checklist2)
                    {
                        checkList.Add(anorder);
                    }
                    SortByPrice(checkList);
                    //排序完成
                    //下面输出
                    foreach (Order anorder in checklist2)
                    {
                        anorder.DisplayAnOrder();
                    }
                    flag = 1;
                    break;
                case 3:
                    Console.WriteLine("要查询的客户名：");
                    checkCustomer = Console.ReadLine();
                    var checklist3 = MyList.Where(x => x.customer == checkCustomer);//用linq来实现查找！
                                                                                    //这里得排序,按照订单总金额
                    foreach (Order anorder in checklist3)
                    {
                        checkList.Add(anorder);
                    }
                    SortByPrice(checkList);
                    //排序完成
                    //下面输出
                    foreach (Order anorder in checklist3)
                    {
                        anorder.DisplayAnOrder();
                    }
                    flag = 1;
                    break;
                case 4:
                    Console.WriteLine("要查询的价格：");
                    s = Console.ReadLine();
                    checkPrice = int.Parse(s);
                    var checklist4 = MyList.Where(x => x.price == checkPrice);//用linq来实现查找！

                    //这里得排序,按照订单总金额
                    foreach (Order anorder in checklist4)
                    {
                        checkList.Add(anorder);
                    }
                    SortByPrice(checkList);
                    //排序完成

                    //下面输出
                    foreach (Order anorder in checklist4)
                    {
                        anorder.DisplayAnOrder();
                    }
                    flag = 1;
                    break;
                default:
                    Console.WriteLine("请输入正确的序号哦！");
                    break;
            }
            if (flag == 1) Console.WriteLine("查询成功！结果已经输出");
            else Console.WriteLine("查询失败！找不到这样的订单");
        }

        public List<Order> SortByPrice(List<Order> list)
        {
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = 0; j < list.Count - i; j++)
                {
                    if (list[j].price > list[j + 1].price)
                    {        // 相邻元素两两对比
                        Order temp = list[j + 1];        // 元素交换
                        list[j + 1] = list[j];
                        list[j] = temp;
                    }
                }
            }
            return list;
        }
        public List<Order> SortByNum(List<Order> list)
        {
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = 0; j < list.Count - 1 - i; j++)
                {
                    if (list[j].num > list[j + 1].num)
                    {        // 相邻元素两两对比
                        Order temp = list[j + 1];        // 元素交换
                        list[j + 1] = list[j];
                        list[j] = temp;
                    }
                }
            }
            return list;
        }
        public void DisplayOrder()
        {
            List<Order> checkList = new List<Order>();
            //先排序，按照订单号顺序！
            checkList = SortByNum(MyList);
            //排序完成

            //下面输出
            foreach (Order anorder in checkList)
            {
                Console.WriteLine("订单号:{0}商品名:{1} 顾客:{2}价格:{3}", anorder.num.ToString(), anorder.thing.ToString(),
                   anorder.customer.ToString(), anorder.price.ToString());
            }
        }
        public void Export()//序列化
        {
            Order[] Orders = new Order[MyList.Count];
            int i = 0;
            foreach (Order anorder in MyList)
            {
                Orders[i++] = anorder;
            }
            Console.WriteLine("这里可以将当前所有订单序列化为xml文件！");

            using (FileStream fs = new FileStream("o.xml", FileMode.Create))
            { ToXML.Serialize(fs, Orders); }


            Console.WriteLine("Serialized as XML:");
            Console.WriteLine(File.ReadAllText("o.xml"));
        }
        public void Import()//反序列化
        {
            Console.WriteLine("这里可以将刚才的xml文件反序列化！");
            using (FileStream fs = new FileStream("o.xml", FileMode.Open))
            {
                Order[] order2 = (Order[])ToXML.Deserialize(fs);
                Console.WriteLine("Deserialized from o.xml");
                Console.WriteLine("载入订单的结果如下:");
                foreach (Order anorder in order2)
                {
                    Console.WriteLine("订单号:" + anorder.num + "顾客:" + anorder.customer +
                        "商品:" + anorder.thing + "价格" + anorder.price);
                }
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            OrderSurvice orderSurvice = new OrderSurvice();
            int opperation = 0;
            string s;
            while (opperation != 99)
            {
                Console.WriteLine("请输入你想进行的操作：1:添加订单 2:删除订单 3:修改订单 4:查询订单 5:输出当前所有订单," +
                    "6:序列化为xml文件 7：反序列化从xml文件中载入订单 退出请输入99");
                s = Console.ReadLine();
                opperation = int.Parse(s);
                switch (opperation)
                {
                    case 1:
                        orderSurvice.AddList();

                        break;
                    case 2:
                        orderSurvice.DeleteList();
                        break;
                    case 3:
                        orderSurvice.ChangeList();
                        break;
                    case 4:
                        orderSurvice.CheckList();
                        break;
                    case 5:
                        orderSurvice.DisplayOrder();
                        break;
                    case 6:
                        orderSurvice.Export();
                        break;
                    case 7:
                        orderSurvice.Import();
                        break;
                    case 99: break;
                    default:
                        Console.WriteLine("请输入正确的数字！");
                        break;
                }
            }

        }
    }
}